<?php
/**
 * @name /www/unit-tests/DonatedItem.php
 * @author Kimberly Berger <knb4850@esu.edu>
 * @version  2011.03.30
 *
 * Unit test for User class.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
// Libraries
ReqLib('/lib/private/Donation.php');

function DefineParams(&$p) {
  $p->Title = 'Donation Unit Test';
}

function DefineHeader() {
?><style type="text/css">
  code {
    font: bold 10pt 'Courier New';
    color: #666;
    background-color: #CCC;
    border: 3px solid #666;
    padding: 4px 6px;
    margin: 10px 20px;
    display: block;
  }
  form, ul, ol {
    margin: 0px 20px;
  }
  a#Back {
    float: right;
    margin: 10px;
    padding: 4px 6px;
    border: 1px solid #666;
    background-color: #CCC;
  }
</style><?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
  /* Methods:

	Donation()

	"GET":
	Donation[] GetDonations(int offset, int maxResults)
	bool Exists(int DonationId)
	string ToString()

	"SCUD":
	bool Load(int donationId)
	bool Create(userId)
	bool Save()
	static bool Delete(int donationId)

	Custom:
	static int[] GetDonationIdsByUserId(userId)
	static int[] GetDonationIdsByItemId(itemId)

	*/
  $actions = array(
    'Delete(id)',
    'Delete All',
    'Create',
    'View Details',
    'List(offset,max)',
    'GetIdsByUserId(id)',
    'GetIdsByItemId(id)',
	'Save');
  // Page content goes here.
  ?>
<h1><a href="DonatedItem.php">Donation - Unit Test</a></h1>
<a id="Back" href="index.php">Back to Unit Testing Home</a>
<form method="GET">
  <label for="Param1">Param 1: </label><input type="text" value="<?=htmlspecialchars($_GET['Param1']) ?>" name="Param1" /><br />
  <label for="Param2">Param 2: </label><input type="text" value="<?=htmlspecialchars($_GET['Param2']) ?>" name="Param2" /><br />
<?
  foreach ($actions as $action)
    println('<input type="submit" name="Action" value="' . $action . '" /><br />');
?><br />
<hr />
<?
  DoAction();
?></form>
<?
}

function DoAction() {
?><p>
<?
  switch ($_GET['Action']) {
    case 'Delete(id)':
      println(Donation::Delete($_GET['Param1']) ? 'Deleted' : 'Not deleted');
      break;

    case 'Delete All':
      $di = Donation::GetDonations();
      foreach ($di as $d)
        Donation::Delete($d->DonationId);
      println('Data cleared.');
      break;

    case 'Create':
      $d = new Donation();
      $di = array(
        array(rand(1,10)),
        array(rand(1,10)),
        array(rand(1,10)),
        array(rand(1,10)),
        array(rand(1,10))
      );
      foreach ($di as $data)
        println("Creating Donation (userid:'{$data[0]}'): " . ($d->Create($data[0]) ? 'Created' : 'Not created') . '<br />');
      break;

    case 'List(offset,max)':
	  print("Displays the DonationId and the UserId");
      println('<ol>');
      foreach(Donation::GetDonations($_GET['Param1'], $_GET['Param2']) as $d)
        println("<li>{$d->DonationId}: {$d->User->UserId}</li>");
		println('</ol>');
      break;

    case 'View Details':
      println('<textarea style="width: 600px; height: 400px;">' . print_r(Donation::GetDonations(0, 1), true) . '</textarea>');
      break;

    case 'GetIdsByUserId(id)':
     println('<textarea style="width: 600px; height: 400px;">'. "ByUserId ('" . htmlspecialchars($_GET['Param1']) . "') = '" . print_r((Donation::GetDonationIdsByUserId($_GET['Param1'])), true) . "' " .  '</textarea>');
	 break;
    case 'GetIdsByItemId(id)':
	  println('<textarea style="width: 600px; height: 400px;">'. "ByItemId ('" . htmlspecialchars($_GET['Param1']) . "') = '" . print_r((Donation::GetDonationIdsByItemId($_GET['Param1'])), true) . "' " . '</textarea>');
      break;

	case 'Save':
      $d = Donation::GetDonations(0, 1);
      if (!count($d)) {
        $d = new Donation();
        $d->Create(rand(1,10), rand(1,10));
      }
      else
      $d = $d[0];
	  $d->DonationId = rand(1, 10);
      $d->Notes = rand(1, 100);
      $d->DateDonated = date('Y-m-d', mktime(0,0,0, rand(1,12), rand(1,28),rand(1000, 5000)));
	  $d->UserId = rand(1,10);
	  if($d->Save())
	  {
		println('Saved');
		println('<br /><textarea style="width: 600px; height: 400px;">DonationId: ' . $d->DonationId . "\r\n" . print_r($d->ToString("\r\n"), true) . '</textarea>');
	  }
	  else
		println('Not saved');

      break;

    default:
      break;
  }
?></p>
<?
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');